package com.icard.wx.icardwx.service.impl;

import com.dsbj.common.model.*;
import com.dsbj.common.msg.ResponseMessage;
import com.icard.wx.icardwx.model.AllowUser;
import com.icard.wx.icardwx.model.WxUser;
import com.icard.wx.icardwx.service.IReservationService;
import com.icard.wx.icardwx.utils.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReservationService implements IReservationService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseMessage<Page<ReservationPrimary>> listByCreatorEmployeeNo(String creatorEmployeeNo,Integer pageNo,Integer pageSize) {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append(String.format("/api/icard/reservation/listByCreatorEmployeeNo?creatorEmployeeNo=%s&pagesize=%d&pagenum=%d",creatorEmployeeNo,pageSize,pageNo));

        ParameterizedTypeReference<ResponseMessage<Page<ReservationPrimary>>> typeRef = new ParameterizedTypeReference<ResponseMessage<Page<ReservationPrimary>>>() {
        };
        ResponseMessage page =  restTemplate.exchange(sb.toString(), HttpMethod.GET,null,typeRef, (Object) null).getBody();
        return page;
    }

    @Override
    public ResponseMessage<String> getReservationNo() {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append("/api/icard/reservation/getNewReservationNo");

        ParameterizedTypeReference<ResponseMessage<String>> typeRef = new ParameterizedTypeReference<ResponseMessage<String>>() {
        };
        ResponseMessage msg =  restTemplate.exchange(sb.toString(), HttpMethod.GET,null,typeRef, (Object) null).getBody();
        return msg;
    }

    @Override
    public ResponseMessage<List<District>> getDistricts() {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append("/api/icard/district/getDistricts");

        ParameterizedTypeReference<ResponseMessage<List<District>>> typeRef = new ParameterizedTypeReference<ResponseMessage<List<District>>>() {
        };
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String, String> params= new HashMap<>();
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<Map<String, String>>(params, headers);
        ResponseMessage msg =  restTemplate.exchange(sb.toString(), HttpMethod.POST,requestEntity,typeRef).getBody();
        return msg;
    }

    @Override
    public ResponseMessage save(WxUser user, ReservationPrimary reservationPrimary) {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append("/api/icard/reservation/save");

        ParameterizedTypeReference<ResponseMessage<String>> typeRef = new ParameterizedTypeReference<ResponseMessage<String>>() {
        };
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String, Object> params= new HashMap<>();
        params.put("user",user);
        params.put("reservationPrimary",reservationPrimary);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(params, headers);
        ResponseMessage msg =  restTemplate.exchange(sb.toString(), HttpMethod.POST,requestEntity,typeRef).getBody();
        return msg;

    }

    @Override
    public ResponseMessage<ReservationPrimary> getReservationPrimaryById(String id) {

        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.getGatewayHost()).append(String.format("/api/icard/reservation/getReservationPrimaryById?id=%s",id));

        ParameterizedTypeReference<ResponseMessage<ReservationPrimary>> typeRef = new ParameterizedTypeReference<ResponseMessage<ReservationPrimary>>() {
        };
        ResponseMessage page =  restTemplate.exchange(sb.toString(), HttpMethod.GET,null,typeRef, (Object) null).getBody();
        return page;
    }

}
